# -*- encoding: utf-8 -*-

import os
import ConfigParser

# Verify if is set 'VERTA_CONFIG' system variable to get config file
if 'VERTA_CONFIG' in os.environ:
    path = os.environ['VERTA_CONFIG']
# Else default place where to get config file is user dir/conf
else:
    path = os.path.expanduser('~/conf')

# Join file name and path
config_file = os.path.sep.join((path, 'verta.conf'))
# Instance Config Parser
parser = ConfigParser.SafeConfigParser()
# If do not find any file raise an Exception
if parser.read(config_file) == []:
    raise Exception, 'Could not found any config files'

def catch(section, key):
    '''Returns key from section of file'''
    return parser.get(section, key)

def get(section, key, default='', instance_of=str):
    '''Returns values trying to parse it into any type, 
    else returning default value'''
    try:
        return instance_of(catch(section, key))
    except ValueError:
        return default

